package tx.bj12320.process;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.activiti.engine.impl.db.DbSqlSession;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.interceptor.CommandExecutor;
import org.activiti.engine.impl.util.CollectionUtil;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.Deployment;
import org.activiti.spring.impl.test.SpringActivitiTestCase;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration("classpath*:spring/*.xml")
public class TestProcess_tousu_zhuanban extends SpringActivitiTestCase {
	private static Logger log = Logger
			.getLogger(TestProcess_tousu_zhuanban.class.getName());

	@Rollback(false)
	@Deployment(resources = "tx/bj12320/process/tousu_zhuanban.bpmn20.xml")
	public void testSetAssigneeThroughSpringService() {

		// Start a process instance
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("Tousu_zhuanban_process");

		List<Task> tasks = taskService.createTaskQuery()
				.taskCandidateUser("banzhang").list();
		log.info(tasks.size() + "");
		for (Task task : tasks) {
			log.info(task.getName());
			taskService.complete(task.getId());
			// processInstance.
		}

		tasks = taskService.createTaskQuery().taskAssignee("xietiaoduban")
				.list();
		log.info(tasks.size() + "");
		for (Task task : tasks) {
			log.info(task.getName());
			Map<String, Object> vars = new HashMap<String, Object>();
			vars.put("isPass", true);
			taskService.complete(task.getId(), vars);
		}

		// runtimeService.deleteProcessInstance(processInstance.getProcessInstanceId(),
		// "��������");
		// processEngine.getHistoryService().createHistoricProcessInstanceQuery().finished();

		tasks = taskService.createTaskQuery().taskAssignee("zhuren").list();
		log.info(tasks.size() + "");
		for (Task task : tasks) {
			log.info(task.getName());
			taskService.complete(task.getId());
		}

		tasks = taskService.createTaskQuery().taskAssignee("xietiaoduban")
				.list();
		log.info(tasks.size() + "");
		for (Task task : tasks) {
			log.info(task.getName());
			taskService.complete(task.getId());
		}
		log.info("");
	}

	private void clean() {

		CommandExecutor commandExecutor = ((ProcessEngineImpl) processEngine)
				.getProcessEngineConfiguration().getCommandExecutorTxRequired();
		commandExecutor.execute(new Command<Object>() {
			public Object execute(CommandContext commandContext) {
				DbSqlSession session = commandContext
						.getSession(DbSqlSession.class);
				session.dbSchemaDrop();
				session.dbSchemaCreate();
				return null;
			}
		});
	}
//	@Override
//	protected void assertAndEnsureCleanDb() throws Throwable {
//		log.info("default not clean db");
//	}

}
